﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using TimKen.DataEntity;

namespace TimKen.DataAccess
{
    /// <summary>
    /// 数据访问类WebUser。
    /// </summary>
    public class WebUserDAO
    {
        public WebUserDAO()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string UserName)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50)};
            parameters[0].Value = UserName;

            int result = DbHelperSQL.RunProcedure("UP_WebUser_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(TimKen.DataEntity.WebUser model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into WebUser(");
            strSql.Append("UserName,NickName,Password,RealName,Company,Phone,Email,IsValid,IsApply,ApplyDate,BankAccount,BusinessLicenseCopy,TaxRegistrationCopy, ShippingAddress,ShippingLinkMan,DeliveryAddress,DeliveryLinkMan)");
            strSql.Append(" values (");
            strSql.Append("@UserName,@NickName,@Password,@RealName,@Company,@Phone,@Email,@IsValid,@IsApply,@ApplyDate,@BankAccount,@BusinessLicenseCopy,@TaxRegistrationCopy,@ShippingAddress,@ShippingLinkMan,@DeliveryAddress,@DeliveryLinkMan)");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,32),
					new SqlParameter("@NickName", SqlDbType.NVarChar,32),
					new SqlParameter("@Password", SqlDbType.VarChar,50),
					new SqlParameter("@RealName", SqlDbType.VarChar,20),
					new SqlParameter("@Company", SqlDbType.VarChar,200),
					new SqlParameter("@Phone", SqlDbType.VarChar,15),
					new SqlParameter("@Email", SqlDbType.VarChar,200),
					new SqlParameter("@IsValid", SqlDbType.Bit,1),
					new SqlParameter("@IsApply", SqlDbType.Bit,1),
					new SqlParameter("@ApplyDate", SqlDbType.DateTime),
					new SqlParameter("@BankAccount", SqlDbType.NVarChar,50),
					new SqlParameter("@BusinessLicenseCopy", SqlDbType.NVarChar,80),
					new SqlParameter("@TaxRegistrationCopy", SqlDbType.NVarChar,80),
				 
					new SqlParameter("@ShippingAddress", SqlDbType.NVarChar,200),
					new SqlParameter("@ShippingLinkMan", SqlDbType.NVarChar,200),
					new SqlParameter("@DeliveryAddress", SqlDbType.NVarChar,200),
					new SqlParameter("@DeliveryLinkMan", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.NickName;
            parameters[2].Value = model.Password;
            parameters[3].Value = model.RealName;
            parameters[4].Value = model.Company;
            parameters[5].Value = model.Phone;
            parameters[6].Value = model.Email;
            parameters[7].Value = model.IsValid;
            parameters[8].Value = model.IsApply;
            parameters[9].Value = model.ApplyDate;
            parameters[10].Value = model.BankAccount;
            parameters[11].Value = model.BusinessLicenseCopy;
            parameters[12].Value = model.TaxRegistrationCopy;
          
            parameters[13].Value = model.ShippingAddress;
            parameters[14].Value = model.ShippingLinkMan;
            parameters[15].Value = model.DeliveryAddress;
            parameters[16].Value = model.DeliveryLinkMan;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.WebUser model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update WebUser set ");
            strSql.Append("NickName=@NickName,");
            strSql.Append("Password=@Password,");
            strSql.Append("RealName=@RealName,");
            strSql.Append("Company=@Company,");
            strSql.Append("Phone=@Phone,");
            strSql.Append("Email=@Email,");
            strSql.Append("IsValid=@IsValid,");
            strSql.Append("IsApply=@IsApply,");
            strSql.Append("ApplyDate=@ApplyDate,");
            strSql.Append("BankAccount=@BankAccount,");
            strSql.Append("BusinessLicenseCopy=@BusinessLicenseCopy,");
            strSql.Append("TaxRegistrationCopy=@TaxRegistrationCopy,");
            strSql.Append("ShippingAddress=@ShippingAddress,");
            strSql.Append("ShippingLinkMan=@ShippingLinkMan,");
            strSql.Append("DeliveryAddress=@DeliveryAddress,");
            strSql.Append("DeliveryLinkMan=@DeliveryLinkMan");
            strSql.Append(" where UserName=@UserName ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,32),
					new SqlParameter("@NickName", SqlDbType.NVarChar,32),
					new SqlParameter("@Password", SqlDbType.VarChar,50),
					new SqlParameter("@RealName", SqlDbType.VarChar,20),
					new SqlParameter("@Company", SqlDbType.VarChar,200),
					new SqlParameter("@Phone", SqlDbType.VarChar,15),
					new SqlParameter("@Email", SqlDbType.VarChar,200),
					new SqlParameter("@IsValid", SqlDbType.Bit,1),
					new SqlParameter("@IsApply", SqlDbType.Bit,1),
					new SqlParameter("@ApplyDate", SqlDbType.DateTime),
					new SqlParameter("@BankAccount", SqlDbType.NVarChar,50),
					new SqlParameter("@BusinessLicenseCopy", SqlDbType.NVarChar,80),
					new SqlParameter("@TaxRegistrationCopy", SqlDbType.NVarChar,80),
					new SqlParameter("@ShippingAddress", SqlDbType.NVarChar,200),
					new SqlParameter("@ShippingLinkMan", SqlDbType.NVarChar,200),
					new SqlParameter("@DeliveryAddress", SqlDbType.NVarChar,200),
					new SqlParameter("@DeliveryLinkMan", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.NickName;
            parameters[2].Value = model.Password;
            parameters[3].Value = model.RealName;
            parameters[4].Value = model.Company;
            parameters[5].Value = model.Phone;
            parameters[6].Value = model.Email;
            parameters[7].Value = model.IsValid;
            parameters[8].Value = model.IsApply;
            parameters[9].Value = model.ApplyDate;
            parameters[10].Value = model.BankAccount;
            parameters[11].Value = model.BusinessLicenseCopy;
            parameters[12].Value = model.TaxRegistrationCopy;
            parameters[13].Value = model.ShippingAddress;
            parameters[14].Value = model.ShippingLinkMan;
            parameters[15].Value = model.DeliveryAddress;
            parameters[16].Value = model.DeliveryLinkMan;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.WebUser GetModel(string UserName)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 UserName,NickName,Password,RealName,Company,Phone,Email,IsValid,IsApply,ApplyDate,BankAccount,BusinessLicenseCopy,TaxRegistrationCopy,CreateDate,ShippingAddress,ShippingLinkMan,DeliveryAddress,DeliveryLinkMan from WebUser ");
            strSql.Append(" where UserName=@UserName ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50)};
            parameters[0].Value = UserName;

            TimKen.DataEntity.WebUser model = new TimKen.DataEntity.WebUser();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                model.NickName = ds.Tables[0].Rows[0]["NickName"].ToString();
                model.Password = ds.Tables[0].Rows[0]["Password"].ToString();
                model.RealName = ds.Tables[0].Rows[0]["RealName"].ToString();
                model.Company = ds.Tables[0].Rows[0]["Company"].ToString();
                model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                if (ds.Tables[0].Rows[0]["IsValid"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsValid"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsValid"].ToString().ToLower() == "true"))
                    {
                        model.IsValid = true;
                    }
                    else
                    {
                        model.IsValid = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["IsApply"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsApply"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsApply"].ToString().ToLower() == "true"))
                    {
                        model.IsApply = true;
                    }
                    else
                    {
                        model.IsApply = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["ApplyDate"].ToString() != "")
                {
                    model.ApplyDate = DateTime.Parse(ds.Tables[0].Rows[0]["ApplyDate"].ToString());
                }
                model.BankAccount = ds.Tables[0].Rows[0]["BankAccount"].ToString();
                model.BusinessLicenseCopy = ds.Tables[0].Rows[0]["BusinessLicenseCopy"].ToString();
                model.TaxRegistrationCopy = ds.Tables[0].Rows[0]["TaxRegistrationCopy"].ToString();
             
                model.ShippingAddress = ds.Tables[0].Rows[0]["ShippingAddress"].ToString();
                model.ShippingLinkMan = ds.Tables[0].Rows[0]["ShippingLinkMan"].ToString();
                model.DeliveryAddress = ds.Tables[0].Rows[0]["DeliveryAddress"].ToString();
                model.DeliveryLinkMan = ds.Tables[0].Rows[0]["DeliveryLinkMan"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string UserName)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50)};
            parameters[0].Value = UserName;

            DbHelperSQL.RunProcedure("UP_WebUser_Delete", parameters, out rowsAffected);
        }
 
        public UserS Login(string userName, string password) 
        {
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50),new SqlParameter("@Password",SqlDbType.VarChar,50)};
            parameters[0].Value = userName;
            parameters[1].Value = password;
            TimKen.DataEntity.WebUser model = new TimKen.DataEntity.WebUser();
            DataSet ds = DbHelperSQL.RunProcedure("UP_WebUser_Login", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                model.NickName = ds.Tables[0].Rows[0]["NickName"].ToString();
                model.Password = ds.Tables[0].Rows[0]["Password"].ToString();
                model.RealName = ds.Tables[0].Rows[0]["RealName"].ToString();
                model.Company = ds.Tables[0].Rows[0]["Company"].ToString();
                model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                if (ds.Tables[0].Rows[0]["IsValid"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsValid"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsValid"].ToString().ToLower() == "true"))
                    {
                        model.IsValid = true;
                    }
                    else
                    {
                        model.IsValid = false;
                    }
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserName,NickName,Password,RealName,Company,Phone,Email,IsValid,IsApply,ApplyDate,BankAccount,BusinessLicenseCopy,TaxRegistrationCopy ");
            strSql.Append(" FROM WebUser ");
            
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string UserName, string RealName, string Company)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@UserName", SqlDbType.VarChar,50),
                    new SqlParameter("@RealName", SqlDbType.VarChar,50),
                    new SqlParameter("@Company", SqlDbType.VarChar,50),
                    };
            parameters[0].Value = PageSize;
            parameters[1].Value = PageIndex;
            parameters[2].Value = UserName;
            parameters[3].Value = RealName;
            parameters[4].Value = Company;
            return DbHelperSQL.RunProcedure("UP_WebUser_GetPageList", parameters, "ds");
        }

        /// <summary>
        /// 是否通过油箱认证
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool IsAuthenticatedUser(string username)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50)};
            parameters[0].Value = username;

            int result = DbHelperSQL.RunProcedure("UP_WebUser_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool AuthenticatedUser(string username) 
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50)};
            parameters[0].Value = username;
            DbHelperSQL.RunProcedure("UP_WebUser_AuthenticatedUser", parameters, out rowsAffected);
            if (rowsAffected > 0) 
            {
                return true;
            }
            return false;
        }
        #endregion  成员方法
    }
}

